package ch.ffhs.esma.bunk_weiss.notepad;

/**
 * @author: Tobias Weiss / Julian Bunk
 * @Date: 04.01.2015
 */

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultCreateNote extends Activity implements OnClickListener {

	String titel;
	String notiztext;
	String datum;
	String zeit;
	String bildpfad;
	String soundpfad;
	DataBaseHandler handler;
	DatabaseAdapter dbadapter;
	SQLiteDatabase database;
	Bundle bundle;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.resultcreatenote);

		// Back to Home
		Button back = (Button) findViewById(R.id.saveButton);
		back.setOnClickListener(this);

		// Back to Create Note Activity
		Button back1 = (Button) findViewById(R.id.cancelButton);
		back1.setOnClickListener(this);

		TextView resultText = (TextView) findViewById(R.id.resultText);

		Bundle bundle = getIntent().getExtras();

		titel = bundle.getString("titel");
		notiztext = bundle.getString("notiztext");
		datum = bundle.getString("datum");
		zeit = bundle.getString("zeit");
		bildpfad = bundle.getString("imageFile");
		soundpfad = bundle.getString("soundFile");

		resultText.setText("\n" + "\n" + "\n" + "Text: " + titel + "\n"
				+ "Notiztext: " + notiztext + "\n" + "Datum: " + datum + "\n"
				+ "Zeit: " + zeit + "\n" + "Bild-Pfad: " + bildpfad + "\n"
				+ "Sound-Pfad: " + soundpfad + "\n");

		// Bild anzeigen
		if (bildpfad != "kein Bild aufgenommen") {
			Bitmap myBitmap = BitmapFactory.decodeFile(bildpfad);
			ImageView myImage = (ImageView) findViewById(R.id.imageView1);
			myImage.setImageBitmap(myBitmap);
		}
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.saveButton) {
			saveToDB();

			// define a new Intent back to the Home Activity
			Intent intent = new Intent(this, Home.class);

			// start the Home Activity
			this.startActivity(intent);
			// Activity beenden
			finish();
		}
		if (arg0.getId() == R.id.cancelButton) {

			// define a new Intent back to the Create Note Activity
			Intent intent = new Intent(this, CreateNote.class);

			// start the Create Note Activity
			this.startActivity(intent);
			// Activity beenden
			finish();
		}
	}

	public void saveToDB() {

		dbadapter = new DatabaseAdapter(this);
		dbadapter.open();
		dbadapter.insertNotiz(titel, notiztext, datum, zeit, bildpfad,
				soundpfad);
		dbadapter.close();
	}
}
